package com.titan.travelagent;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.titan.domain.Cabin;
import com.titan.domain.Cruise;
import com.titan.domain.Customer;
import com.titan.domain.CustomerPK;
import com.titan.domain.Reservation;
import com.titan.domain.Ship;

/**
 * Session Bean implementation class TravelAgentBean
 */
@Stateless
public class TravelAgentBean implements TravelAgentBeanRemote {

	@PersistenceContext(unitName="titan") private EntityManager manager; 
	
	public void createCabin(Cabin cabin){
		manager.persist(cabin);
	}
	
	public Cabin findCabin(int pKey){
		return manager.find(Cabin.class, pKey);
	}
	
	public void createReservation(Reservation reservation){
		manager.persist(reservation);
	}

	public void createCustomer(Customer cust){
		manager.persist(cust);
	}
	
	public Customer findCustomer(CustomerPK pKey){
		return manager.find(Customer.class, pKey);
	}
	
	public void createShip(Ship ship){
		manager.persist(ship);
	}
	public Ship findShipByName(String name){
		try{
		Query query = manager.createQuery("Select s from Ship s where s.name='"+name+"'");
		return (Ship)query.getSingleResult();
		}catch (NoResultException ex)
		{
			return null;
		}
	}
	
	public void createCruise(Cruise cruise){
		System.out.println("System Out in statelessbean");
		manager.persist(cruise);
	}
	
	@SuppressWarnings("unchecked")
	public List<Long> findSsnCustomersLongLastNames(){
		Query query=manager.createNamedQuery("testQuery");
		return (List<Long>) query.getResultList();
	}
}
